using BXS.API.Models;
using Microsoft.AspNetCore.Mvc;

namespace BXS.API.Controllers
{
    /// <summary>
    /// 轮播图管理控制器
    /// </summary>
    public class BannersController : BaseController
    {
        // 模拟数据
        private static List<Banner> _banners = new List<Banner>
        {
            new Banner { Id = 1, Title = "白喜事服务", Image = "https://via.placeholder.com/400x200", Link = "/category", Sort = 1, Status = "active", CreateTime = "2024-01-01" },
            new Banner { Id = 2, Title = "专业殡葬服务", Image = "https://via.placeholder.com/400x200", Link = "/service", Sort = 2, Status = "active", CreateTime = "2024-01-02" },
            new Banner { Id = 3, Title = "24小时服务热线", Image = "https://via.placeholder.com/400x200", Link = "/contact", Sort = 3, Status = "active", CreateTime = "2024-01-03" },
            new Banner { Id = 4, Title = "清明节祭扫服务", Image = "https://via.placeholder.com/400x200", Link = "/qingming", Sort = 4, Status = "inactive", CreateTime = "2024-01-04" },
        };

        /// <summary>
        /// 获取轮播图列表
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="status">状态筛选</param>
        /// <returns>轮播图列表</returns>
        [HttpGet]
        public ActionResult<PagedApiResponse<Banner>> GetBanners([FromQuery] int page = 1, [FromQuery] int pageSize = 10, [FromQuery] string? status = null)
        {
            var query = _banners.AsQueryable();
            
            if (!string.IsNullOrEmpty(status) && status != "all")
            {
                query = query.Where(b => b.Status == status);
            }

            var total = query.Count();
            var skip = (page - 1) * pageSize;
            var banners = query.OrderBy(b => b.Sort).Skip(skip).Take(pageSize).ToList();
            
            return PagedSuccess(banners, total, page, pageSize);
        }

        /// <summary>
        /// 获取活跃的轮播图列表（用于前端展示）
        /// </summary>
        /// <returns>活跃轮播图列表</returns>
        [HttpGet("active")]
        public ActionResult<ApiResponse<List<Banner>>> GetActiveBanners()
        {
            var activeBanners = _banners.Where(b => b.Status == "active").OrderBy(b => b.Sort).ToList();
            return Success(activeBanners);
        }

        /// <summary>
        /// 根据ID获取轮播图
        /// </summary>
        /// <param name="id">轮播图ID</param>
        /// <returns>轮播图信息</returns>
        [HttpGet("{id}")]
        public ActionResult<ApiResponse<Banner>> GetBanner(int id)
        {
            var banner = _banners.FirstOrDefault(b => b.Id == id);
            if (banner == null)
            {
                return Error<Banner>("轮播图不存在", 404);
            }
            return Success(banner);
        }

        /// <summary>
        /// 创建轮播图
        /// </summary>
        /// <param name="request">创建轮播图请求</param>
        /// <returns>创建结果</returns>
        [HttpPost]
        public ActionResult<ApiResponse<Banner>> CreateBanner([FromBody] CreateBannerRequest request)
        {
            if (string.IsNullOrEmpty(request.Title) || string.IsNullOrEmpty(request.Image))
            {
                return Error<Banner>("请填写完整信息");
            }

            var newBanner = new Banner
            {
                Id = _banners.Count > 0 ? _banners.Max(b => b.Id) + 1 : 1,
                Title = request.Title,
                Image = request.Image,
                Link = request.Link,
                Sort = request.Sort,
                Status = request.Status,
                CreateTime = DateTime.Now.ToString("yyyy-MM-dd")
            };

            _banners.Add(newBanner);
            return Success(newBanner, "轮播图创建成功");
        }

        /// <summary>
        /// 更新轮播图
        /// </summary>
        /// <param name="id">轮播图ID</param>
        /// <param name="request">更新轮播图请求</param>
        /// <returns>更新结果</returns>
        [HttpPut("{id}")]
        public ActionResult<ApiResponse<Banner>> UpdateBanner(int id, [FromBody] UpdateBannerRequest request)
        {
            var banner = _banners.FirstOrDefault(b => b.Id == id);
            if (banner == null)
            {
                return Error<Banner>("轮播图不存在", 404);
            }

            if (string.IsNullOrEmpty(request.Title) || string.IsNullOrEmpty(request.Image))
            {
                return Error<Banner>("请填写完整信息");
            }

            banner.Title = request.Title;
            banner.Image = request.Image;
            banner.Link = request.Link;
            banner.Sort = request.Sort;
            banner.Status = request.Status;

            return Success(banner, "轮播图更新成功");
        }

        /// <summary>
        /// 删除轮播图
        /// </summary>
        /// <param name="id">轮播图ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete("{id}")]
        public ActionResult<ApiResponse<bool>> DeleteBanner(int id)
        {
            var banner = _banners.FirstOrDefault(b => b.Id == id);
            if (banner == null)
            {
                return Error<bool>("轮播图不存在", 404);
            }

            _banners.Remove(banner);
            return Success(true, "轮播图删除成功");
        }

        /// <summary>
        /// 获取轮播图状态列表
        /// </summary>
        /// <returns>状态列表</returns>
        [HttpGet("statuses")]
        public ActionResult<ApiResponse<List<string>>> GetStatuses()
        {
            var statuses = _banners.Select(b => b.Status).Distinct().ToList();
            return Success(statuses);
        }
    }
} 